using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bingo.API.UI;
using Bingo.FairyGUI;
using CatLib;
using FairyGUI;
using GamePackage;
using QFramework;
using UnityEngine;

namespace Bingo.UI
{
    public class FairyGUISystem : IUI
    {
        private FairyGUIManager _uiManager;

        public FairyGUISystem()
        {
        }

        private UI_UILoading _uiLoading;

        public void Init()
        {
            GameObject go = new GameObject();
            go.name = "UIRoot";
            _uiManager = go.AddComponent<FairyGUIManager>();

#if UNITY_EDITOR
            UIPackage.AddPackage($"{FairyGUIManager.FUI_PACKAGE_DIR}/gamepackage");
#else
            AssetBundle assetBundle = (AssetBundle)Bingo.Facades.Resources.LoadSync("gamepackage");
            UIPackage.AddPackage(assetBundle);
#endif

            GamePackageBinder.BindAll();

            /*App.On(EventConst.EVENT_SHOW_LOADING, OnShowLoadingHandler);
            App.On(EventConst.EVENT_HIDE_LOADING, OnHideLoadingHandler);*/
        }

        private bool _showLoading = false;


        public async Task ShowLoading()
        {
            _showLoading = true;
            await _uiManager.ShowLoading();
        }

        public async Task HideLoading()
        {
            await _uiManager.HideLoading();
        }

        public void OpenUI<T>(string uiBehaviourName, IUIData uiData = null, string assetBundleName = null)
            where T : UIBasePanel
        {
            _uiManager.OpenUI<T>(uiBehaviourName, uiData, assetBundleName);
        }

        public void CreateUI<T>(string uiBehaviourName, IUIData uiData = null, string assetBundleName = null)
            where T : UIBasePanel
        {
            _uiManager.CreateUI<T>(uiBehaviourName, uiData, assetBundleName);
        }

        public void ShowUI<T>() where T : UIBasePanel
        {
            _uiManager.ShowUI<T>();
        }


        public void HideUI<T>() where T : UIBasePanel
        {
            _uiManager.HideUI<T>();
        }

        public void CloseUI<T>() where T : UIBasePanel
        {
            _uiManager.CloseUI<T>();
        }

        public void CloseUI(string name)
        {
            _uiManager.CloseUI(name);
        }

        public T GetUI<T>() where T : UIBasePanel
        {
            return _uiManager.GetUI<T>();
        }
    }
}